<?php
/**
 * Created by PhpStorm.
 * User: lz
 * Date: 2017/8/28
 * Time: 14:14
 */

namespace app\modules\customer\controllers;


use app\common\service\ConstantMapService;
use app\models\yuyue\Yuyue;
use app\models\zuoye\Zuoye;
use app\modules\customer\common\BaseController;

class YuyueController extends BaseController
{
    /**预约主页
     * @return string
     */
    public function actionIndex()
    {

        if (\Yii::$app->request->isGet) {
            $zuoye_info_list = Zuoye::find()->where(["cat_id" => 1])->all();

            $this->layout = false;
            return $this->render("index", [
                "zuoye_info_list" => $zuoye_info_list
            ]);
        }

        $postData = file_get_contents('php://input', true);
        $obj = json_decode($postData, true);
        $information = $obj['information'];

        $user_info = \Yii::$app->view->params['current_user'];

        for ($i = 0; $i < count($information); $i++) {
            $model = new Yuyue();
            $model->name = $information[$i]['item'];
            $model->member_id = $user_info['id'];
            $model->member_nickname = $user_info['nickname'];
            $model->phone = $information[$i]['phone'];
            $model->time = $information[$i]['time'];
            $model->remark = $information[$i]['remarks'];
            $model->price = $information[$i]['price'];

            $model->save(0);
        }

        return $this->renderJson([], "预约成功", 200);

    }

    public function actionZuoye()
    {
        $id = $this->post("id", "");

        if (!$id) {
            return $this->renderJson([], ConstantMapService::$default_syserror, -1);
        }

        $zuoye_info_list = Zuoye::find()->where(["cat_id" => $id])->all();

        return $this->renderJson([
            "zuoye_info_list" => $zuoye_info_list
        ], "success", 200);
    }

}